/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.index.BrowseTerm;
/*     */ import com.zimbra.cs.index.DomainBrowseTerm;
/*     */ import com.zimbra.cs.index.DomainBrowseTerm.Field;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mailbox.BrowseBy;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BrowseWrapper
/*     */   extends PhantomResource
/*     */ {
/*     */   private BrowseBy mAction;
/*     */   
/*     */   private static enum BrowseBy
/*     */   {
/*  44 */     menu,  date,  type,  sender;
/*     */     
/*     */     private BrowseBy() {}
/*     */   }
/*     */   
/*     */   BrowseWrapper(String uri, String owner) {
/*  50 */     this(uri, owner, parseUri(uri));
/*     */   }
/*     */   
/*     */   BrowseWrapper(String uri, String owner, List<String> tokens) {
/*  54 */     super(uri, owner, tokens);
/*     */     
/*  56 */     String name = (String)this.mTokens.get(this.mTokens.size() - 1);
/*  57 */     if (tokens.size() == 1) {
/*  58 */       this.mAction = BrowseBy.menu;
/*  59 */     } else if (name.equals("by-date")) {
/*  60 */       this.mAction = BrowseBy.date;
/*  61 */     } else if (name.equals("by-type")) {
/*  62 */       this.mAction = BrowseBy.type;
/*  63 */     } else if (name.equals("by-sender")) {
/*  64 */       this.mAction = BrowseBy.sender;
/*     */     } else {
/*  66 */       this.mAction = BrowseBy.date;
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean isCollection()
/*     */   {
/*  72 */     return true;
/*     */   }
/*     */   
/*     */   public List<DavResource> getChildren(DavContext ctxt)
/*     */   {
/*     */     try {
/*  78 */       switch (this.mAction) {
/*     */       case menu: 
/*  80 */         return generateMenu(ctxt);
/*     */       case date: 
/*  82 */         return browseByDate(ctxt);
/*     */       case sender: 
/*  84 */         return browseBySender(ctxt);
/*     */       case type: 
/*  86 */         return browseByType(ctxt);
/*     */       }
/*     */     } catch (ServiceException e) {
/*  89 */       ZimbraLog.dav.error("can't generate browse resource: uri=" + getUri(), e);
/*     */     } catch (IOException e) {
/*  91 */       ZimbraLog.dav.error("can't generate browse resource: uri=" + getUri(), e);
/*     */     }
/*  93 */     return Collections.emptyList();
/*     */   }
/*     */   
/*     */   private String generateUri(String path) {
/*  97 */     StringBuilder buf = new StringBuilder();
/*  98 */     buf.append(getUri());
/*  99 */     if (buf.charAt(buf.length() - 1) != '/')
/* 100 */       buf.append("/");
/* 101 */     buf.append(path);
/* 102 */     return buf.toString();
/*     */   }
/*     */   
/*     */   private List<DavResource> generateMenu(DavContext ctxt) {
/* 106 */     ArrayList<DavResource> menu = new ArrayList();
/* 107 */     menu.add(new BrowseWrapper(generateUri("by-date"), getOwner()));
/* 108 */     menu.add(new BrowseWrapper(generateUri("by-type"), getOwner()));
/* 109 */     menu.add(new BrowseWrapper(generateUri("by-sender"), getOwner()));
/* 110 */     return menu;
/*     */   }
/*     */   
/*     */   private List<DavResource> browseByDate(DavContext ctxt) {
/* 114 */     ArrayList<DavResource> res = new ArrayList();
/* 115 */     res.add(new SearchWrapper(generateUri("today"), getOwner()));
/* 116 */     res.add(new SearchWrapper(generateUri("last-week"), getOwner()));
/* 117 */     res.add(new SearchWrapper(generateUri("last-month"), getOwner()));
/* 118 */     res.add(new SearchWrapper(generateUri("last-year"), getOwner()));
/*     */     
/* 120 */     return res;
/*     */   }
/*     */   
/*     */   private List<DavResource> browseBySender(DavContext ctxt) throws IOException, ServiceException {
/* 124 */     ArrayList<DavResource> res = new ArrayList();
/* 125 */     String user = ctxt.getUser();
/* 126 */     Provisioning prov = Provisioning.getInstance();
/* 127 */     Account account = prov.get(Key.AccountBy.name, user);
/* 128 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 129 */     List<BrowseTerm> terms = mbox.browse(ctxt.getOperationContext(), Mailbox.BrowseBy.domains, "", 0);
/* 130 */     for (BrowseTerm term : terms) {
/* 131 */       if ((term instanceof DomainBrowseTerm)) {
/* 132 */         DomainBrowseTerm domain = (DomainBrowseTerm)term;
/* 133 */         if (domain.contains(DomainBrowseTerm.Field.FROM)) {
/* 134 */           res.add(new BrowseWrapper(generateUri(domain.getText()), getOwner()));
/*     */         }
/*     */       }
/*     */     }
/* 138 */     return res;
/*     */   }
/*     */   
/*     */   private List<DavResource> browseByType(DavContext ctxt) throws IOException, ServiceException {
/* 142 */     ArrayList<DavResource> res = new ArrayList();
/* 143 */     String user = ctxt.getUser();
/* 144 */     Provisioning prov = Provisioning.getInstance();
/* 145 */     Account account = prov.get(Key.AccountBy.name, user);
/* 146 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 147 */     List<BrowseTerm> terms = mbox.browse(ctxt.getOperationContext(), Mailbox.BrowseBy.attachments, "", 0);
/* 148 */     for (BrowseTerm term : terms) {
/* 149 */       String ctype = term.getText();
/* 150 */       int index = ctype.indexOf('/');
/* 151 */       if ((index == -1) && (!ctype.equals("message")) && (!ctype.equals("none")))
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 157 */         res.add(new BrowseWrapper(generateUri(ctype), getOwner())); }
/*     */     }
/* 159 */     res.add(new BrowseWrapper(generateUri("word"), getOwner()));
/* 160 */     res.add(new BrowseWrapper(generateUri("excel"), getOwner()));
/* 161 */     res.add(new BrowseWrapper(generateUri("ppt"), getOwner()));
/* 162 */     res.add(new BrowseWrapper(generateUri("pdf"), getOwner()));
/* 163 */     return res;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/BrowseWrapper.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */